﻿Imports UtilPadrao
Imports Controle
Public Class frmCopiarPermissoes

#Region "Variáveis"

    Private objGrupoAcesso As New ctrGrupoAcesso
    Private datMenus As New DataTable

#End Region

#Region "Propriedades"

    Public Property codigoSelecionado As Int32
    Public Property codigoModulo As Int32
    Public Property nomeModulo As String
    Public Property datSelecao As New DataTable

#End Region

#Region "Eventos"

    Private Sub frmCopiarPermissoes_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Try

            Me.datSelecao = Me.objGrupoAcesso.selecionarGrupoAcessoMenus(0, 0)
            Me.datMenus = Me.datSelecao.Copy
            Me.txtModulo.Text = Me.nomeModulo

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub btnGrupoAcesso_Click(sender As System.Object, e As System.EventArgs) Handles btnGrupoAcesso.Click

        Dim datGrupoAcesso As New DataTable
        Dim objAuxiliar As Object()

        Try

            datGrupoAcesso = Me.objGrupoAcesso.selecionarGrupoAcesso(Me.codigoModulo, Me.codigoSelecionado)

            If datGrupoAcesso.Rows.Count > 0 Then

                objAuxiliar = FronteiraPadrao.frmPesquisa.executarPesquisaGenerica("Selecionar Grupo de Acesso",
                                                                                    Me.objGrupoAcesso.sCamposTabela,
                                                                                    Me.objGrupoAcesso.sCamposDemonstracao,
                                                                                    Me.objGrupoAcesso.sCamposChave,
                                                                                    Me.objGrupoAcesso.sTitulosColunas,
                                                                                    datGrupoAcesso)
                If Not Verificador.seVazio(objAuxiliar) Then
                    Me.txtCodGrupo.Text = objAuxiliar(0)
                    Me.txtGrupoAcesso.Text = objAuxiliar(1)
                    Me.carregarPermissoes()
                Else
                    Me.limparCampos()
                End If

            Else
                MessageBox.Show("Não existem grupos de acesso cadastrados para a pesquisa.",
                                Me.Text,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation)
                Me.limparCampos()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub txtCodGrupo_LostFocus(sender As Object, e As System.EventArgs) Handles txtCodGrupo.LostFocus

        Dim drGrupo As DataRow

        Try

            If Not String.IsNullOrEmpty(Me.txtCodGrupo.Text) Then

                drGrupo = Me.objGrupoAcesso.selecionarGrupoAcessoCodigo(conversao.toInt32(Me.txtCodGrupo.Text),
                                                                        Me.codigoModulo,
                                                                        Me.codigoSelecionado)

                If Not drGrupo Is Nothing Then
                    Me.txtCodGrupo.Text = drGrupo.Item("codGrupo")
                    Me.txtGrupoAcesso.Text = drGrupo.Item("nome")
                    Me.carregarPermissoes()
                Else
                    MessageBox.Show("Identificador inválido.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.limparCampos()
                End If

            Else
                Me.limparCampos()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub btnSelecionar_Click(sender As System.Object, e As System.EventArgs) Handles btnSelecionar.Click

        Try

            Me.datSelecao = Me.datMenus.Copy
            Me.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub carregarPermissoes()

        Try

            Me.datMenus = Me.objGrupoAcesso.selecionarGrupoAcessoMenus(conversao.toInt32(Me.txtCodGrupo.Text),
                                                                       Me.codigoModulo)

            Me.dgvGrid.DataSource = Me.datMenus

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub btnSair_Click(sender As System.Object, e As System.EventArgs) Handles btnSair.Click
        Me.Close()
    End Sub

    Private Sub limparCampos()

        Me.txtCodGrupo.Text = String.Empty
        Me.txtGrupoAcesso.Text = String.Empty
        Me.datMenus.Rows.Clear()
        Me.dgvGrid.DataSource = Me.datMenus

    End Sub

#End Region




End Class